//
//  UserAddressCell.m
//  danpin
//
//  Created by chuangjia on 26/10/2015.
//  Copyright © 2015 chuangjia. All rights reserved.
//

#import "UserAddressCell.h"

@interface UserAddressCell ()
{
    
}
@property(nonatomic,strong)UILabel *nameLabel;
@property(nonatomic,strong)UILabel *phoneLabel;
@property(nonatomic,strong)UILabel *addressLabel;
@property(nonatomic,strong)UIView *lineview;

@property(nonatomic,strong) UIButton *defaultbtn;
@property(nonatomic,strong) UIButton *editbtn;
@property(nonatomic,strong) UIButton *deletebtn;
@end
@implementation UserAddressCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor=[UIColor clearColor];
  
        _nameLabel =[[UILabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH/2,20)];
        _nameLabel.backgroundColor = [UIColor clearColor];
        _nameLabel.textColor = [UIColor darkGrayColor];
        _nameLabel.textAlignment = NSTextAlignmentLeft;
        _nameLabel.font = FONT(14);
        [self.contentView addSubview:_nameLabel];
        _phoneLabel =[[UILabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH/2,20)];
        _phoneLabel.backgroundColor = [UIColor clearColor];
        _phoneLabel.textColor = [UIColor darkGrayColor];
        _phoneLabel.textAlignment = NSTextAlignmentRight;
        _phoneLabel.font = FONT(14);
        [self.contentView addSubview:_phoneLabel];
        
        _addressLabel =[[UILabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH,20)];
        _addressLabel.backgroundColor = [UIColor clearColor];
        _addressLabel.textColor = [UIColor darkGrayColor];
        _addressLabel.textAlignment = NSTextAlignmentLeft;
        _addressLabel.font = FONT(14);
        [self.contentView addSubview:_addressLabel];
        
        _lineview =[[UIView alloc] initWithFrame:CGRectZero];
        _lineview.backgroundColor=UIColorFromRGB(0xcccccc);
        [self.contentView addSubview:_lineview];
        
        _defaultbtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _defaultbtn.tag=0;
        _defaultbtn.titleLabel.font = FONT(12);
        _defaultbtn.layer.borderColor =UIColorFromRGB(0xcccccc).CGColor;
        _defaultbtn.layer.borderWidth=1;
        [_defaultbtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [_defaultbtn addTarget:self action:@selector(NumberAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_defaultbtn];
        
        _editbtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _editbtn.tag=1;
       _editbtn.titleLabel.font = FONT(12);
        _editbtn.layer.borderColor =UIColorFromRGB(0xcccccc).CGColor;
        _editbtn.layer.borderWidth=1;
        [_editbtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
         [_editbtn setTitle:@"编辑" forState:UIControlStateNormal];
        [_editbtn addTarget:self action:@selector(NumberAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_editbtn];
        
        _deletebtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _deletebtn.tag=2;
        _deletebtn.layer.borderColor =UIColorFromRGB(0xcccccc).CGColor;
        _deletebtn.layer.borderWidth=1;
        _deletebtn.titleLabel.font = FONT(12);
        [_deletebtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [_deletebtn setTitle:@"删除" forState:UIControlStateNormal];
       
        [_deletebtn addTarget:self action:@selector(NumberAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_deletebtn];
        
    }
    return self;
}
-(void)dealloc
{
    self.actionBlock =nil;
    self.data=nil;
    self.indexPath=nil;
    self.other=nil;
    self.key=nil;

    [self.nameLabel removeFromSuperview];
    self.nameLabel=nil;
    [self.phoneLabel removeFromSuperview];
    self.phoneLabel=nil;
    [self.addressLabel removeFromSuperview];
    self.addressLabel=nil;
    self.lineview=nil;
    //    DLog(@"%s", __PRETTY_FUNCTION__);
}
- (void)drawRect:(CGRect)rect {
    
    [super drawRect:rect];
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
}
/// 根据数据模型来显示内容
- (void)showInfo:(id)model key:(id)key indexPath:(NSIndexPath *)indexPath
{
    self.key =key;
    self.indexPath=indexPath;
    NSDictionary *dict =(NSDictionary*)model;
    
    self.data =model;
    _nameLabel.text =[[NSString stringWithFormat:@"%@",[dict objectForKey:@"real_name"]] replaceNullString];
    CGSize size = [_nameLabel boundingRectWithSize:CGSizeMake(PHOTO_FRAME_WIDTH*10, 20)];
    _nameLabel.frame =CGRectMake(PHOTO_FRAME_WIDTH, PHOTO_FRAME_WIDTH,size.width, 20);
    //
    _phoneLabel.text =[[NSString stringWithFormat:@"%@",[dict objectForKey:@"phone"]] replaceNullString];
    CGSize size1 = [_phoneLabel boundingRectWithSize:CGSizeMake(PHOTO_FRAME_WIDTH*10, 20)];
    _phoneLabel.frame =CGRectMake(_nameLabel.frame.size.width+_nameLabel.frame.origin.x+PHOTO_FRAME_WIDTH, PHOTO_FRAME_WIDTH,size1.width, 20);
    //
    
    NSString *provinceStr = [[NSString stringWithFormat:@"%@",[dict objectForKey:@"province"]] replaceNullString];
    NSString *cityStr = [[NSString stringWithFormat:@"%@",[dict objectForKey:@"city"]] replaceNullString];
    NSString *districtStr = [[NSString stringWithFormat:@"%@",[dict objectForKey:@"town"]] replaceNullString];
    NSString *detail = [[NSString stringWithFormat:@"%@",[dict objectForKey:@"detail_address"]] replaceNullString];
    NSString *showMsg = [NSString stringWithFormat: @"%@%@%@%@", provinceStr, cityStr, districtStr,detail];
    
    _addressLabel.frame =CGRectMake(PHOTO_FRAME_WIDTH, 35,DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2, 20);
    _addressLabel.text =showMsg;
 //
    _lineview.frame =CGRectMake(PHOTO_FRAME_WIDTH, 65,DEVICE_WIDTH, 0.5);
    //
    int is_default = [[dict objectForKey:@"is_default"] intValue];
    _defaultbtn.frame =CGRectMake(PHOTO_FRAME_WIDTH, 72,80, 20);
    if (is_default) {
         [_defaultbtn setTitle:@"默认地址" forState:UIControlStateNormal];
        _defaultbtn.layer.borderColor =[UIColor redColor].CGColor;
        _defaultbtn.layer.borderWidth=1;
        [_defaultbtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    }else{
         [_defaultbtn setTitle:@"设置默认地址" forState:UIControlStateNormal];
        _defaultbtn.layer.borderColor =UIColorFromRGB(0xcccccc).CGColor;
        _defaultbtn.layer.borderWidth=1;
        [_defaultbtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    }
    _deletebtn.frame =CGRectMake(DEVICE_WIDTH -55, 72,45, 20);
    _editbtn.frame =CGRectMake(_deletebtn.frame.origin.x -55, 72,45, 20);
    [self layoutSubviews];
}
-(void)setCallBack:(void(^)(BEventType,UIView*,id,id,NSIndexPath *))callback
{
    self.actionBlock=callback;
}

-(void)NumberAction:(id)sender
{
    UIButton *btn = (UIButton *)sender;
    BEventType type = BEventType_Unknow;
    switch (btn.tag) {
        case 0:
            type =BEventType_addressdefault;
            break;
        case 1:
            type =BEventType_addressedit;
            break;
        case 2:
            type =BEventType_addressdelete;
            break;
        default:
            break;
    }
    if (self.actionBlock) {
        self.actionBlock(type,sender,self.data,nil,self.indexPath);
    }
}
/// 返回Cell高度
+ (CGFloat)returnCellHeight:(id)model
{
    return PHOTO_FRAME_WIDTH*10;
}
@end
